/**
 * Created by zhouxiahao on 18/08/07.
 */

import service from './request'
// import axios from 'axios'
// 配合promise异步链式调用
export function fetch(data) {
  console.log(1)
  return new Promise((resolve, reject) => {
    requestMethod(data)
      .then(response => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}
// 封装多类请求
export function requestMethod({
  method,
  url,
  _url = false,
  data = {},
  headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}) {
  console.log(2)
  if (url == null) {
    console.error('url不能为空')
    return
  }
  if (_url) {
    service.defaults.baseURL = _url
  }
  return service({
    url: url,
    method: method,
    data: data,
    transformRequest: [function(data) {
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: headers
  })
}
